package com.xique.door.project.xyl.controller;

import com.xique.common.core.utils.poi.ExcelUtil;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.page.TableDataInfo;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.door.project.xyl.bean.domain.XylAlarmRecord;
import com.xique.door.project.xyl.bean.vo.SummaryStatisticsVo;
import com.xique.door.project.xyl.service.IXylAlarmRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author: caogq
 * @Date: 2021/12/27 17:07
 * @Description:
 */
@Controller
@RequestMapping("/xyl/record")
public class XylAlarmRecordController extends BaseController {
    private String prefix = "oms/record";

    @Autowired
    private IXylAlarmRecordService xylAlarmRecordService;

    @PreAuthorize(hasPermi = "oms:record:view")
    @GetMapping()
    public String record() {
        return prefix + "/record";
    }

    /**
     * 查询西洋楼报警记录列表
     */
    @PreAuthorize(hasPermi = "oms:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(XylAlarmRecord xylAlarmRecord) {
        startPage();
        List<XylAlarmRecord> list = xylAlarmRecordService.selectXylAlarmRecordList(xylAlarmRecord);
        return getDataTable(list);
    }

    /**
     * 导出西洋楼报警记录列表
     */
    @PreAuthorize(hasPermi = "oms:record:export")
    @PostMapping("/export")
    @ResponseBody
    public void export(XylAlarmRecord xylAlarmRecord, HttpServletResponse response) throws IOException {
        List<XylAlarmRecord> list = xylAlarmRecordService.selectXylAlarmRecordList(xylAlarmRecord);
        ExcelUtil<XylAlarmRecord> util = new ExcelUtil<XylAlarmRecord>(XylAlarmRecord.class);
        util.exportExcel(response, list, "record");
    }

    /**
     * 新增西洋楼报警记录
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存西洋楼报警记录
     */
    @PreAuthorize(hasPermi = "oms:record:add")
    @Log(title = "西洋楼报警记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(XylAlarmRecord xylAlarmRecord) {
        return toAjax(xylAlarmRecordService.insertXylAlarmRecord(xylAlarmRecord));
    }

    /**
     * 修改西洋楼报警记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        XylAlarmRecord xylAlarmRecord = xylAlarmRecordService.selectXylAlarmRecordById(id);
        mmap.put("xylAlarmRecord", xylAlarmRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存西洋楼报警记录
     */
    @PreAuthorize(hasPermi = "oms:record:edit")
    @Log(title = "西洋楼报警记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(XylAlarmRecord xylAlarmRecord) {
        return toAjax(xylAlarmRecordService.updateXylAlarmRecord(xylAlarmRecord));
    }

    /**
     * 删除西洋楼报警记录
     */
    @PreAuthorize(hasPermi = "oms:record:remove")
    @Log(title = "西洋楼报警记录", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(xylAlarmRecordService.deleteXylAlarmRecordByIds(ids));
    }

    @Log(title = "西洋楼报警记录定时统计", businessType = BusinessType.INSERT)
    @GetMapping("/alarm")
    @ResponseBody
    public AjaxResult alarmStatistics() {
        try {
            xylAlarmRecordService.alarmStatistics();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success();
    }
}
